package com.nbx.app.neheport.render;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.nbx.library.glutes.util;

import android.content.Context;
import android.opengl.GLU;

public class Render05 extends RenderBase {

    private float rotx = 0.0f;
    private float roty = 0.0f;
     
    private float vertexPyramid[] = { 
             0.0f, 0.5f,  0.0f,
            
            -0.5f,-0.5f,  0.5f,
             0.5f,-0.5f,  0.5f,
             0.5f,-0.5f, -0.5f,
            -0.5f,-0.5f, -0.5f,
            -0.5f,-0.5f,  0.5f,
        };
    
    private float colorsPyramid[] = { 
            1.0f, 0.0f, 0.0f, 1.0f, 
            0.0f, 1.0f, 0.0f, 1.0f, 
            0.0f, 0.0f, 1.0f, 1.0f, 
            1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 1.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, 1.0f};

    private float vertexCube[] = {
            // FRONT
            -0.5f, -0.5f,  0.5f,
             0.5f, -0.5f,  0.5f,
            -0.5f,  0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
            // BACK
            -0.5f, -0.5f, -0.5f,
            -0.5f,  0.5f, -0.5f,
             0.5f, -0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
            // LEFT
            -0.5f, -0.5f,  0.5f,
            -0.5f,  0.5f,  0.5f,
            -0.5f, -0.5f, -0.5f,
            -0.5f,  0.5f, -0.5f,
            // RIGHT
             0.5f, -0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
             0.5f, -0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
            // TOP
            -0.5f,  0.5f,  0.5f,
             0.5f,  0.5f,  0.5f,
             -0.5f,  0.5f, -0.5f,
             0.5f,  0.5f, -0.5f,
            // BOTTOM
            -0.5f, -0.5f,  0.5f,
            -0.5f, -0.5f, -0.5f,
             0.5f, -0.5f,  0.5f,
             0.5f, -0.5f, -0.5f,
            };

     
    private FloatBuffer mPyramidVetexBuffer;
    private FloatBuffer mPyramidColorBuffer;
    
    private FloatBuffer mCubeVetexBuffer;
    
    public Render05(Context context) {
        super(context);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        mPyramidVetexBuffer = util.createFloatBuffer(vertexPyramid);
        mPyramidColorBuffer = util.createFloatBuffer(colorsPyramid);
        mCubeVetexBuffer = util.createFloatBuffer(vertexCube);
        
        gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
        
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
        gl.glClearDepthf(1.0f); // Depth Buffer Setup
        
        gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
        gl.glDepthFunc(GL10.GL_LEQUAL);// The Type Of Depth Testing To Do
        
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  // Really Nice Perspective Calculations
    }
    
    public void onDrawFrame(GL10 gl) {        
        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
        gl.glLoadIdentity();

        GLU.gluLookAt(gl, 0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

        // Pyramid
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        gl.glPushMatrix();
        gl.glTranslatef(-1.0f, 0.0f, -5.0f);
        gl.glRotatef(rotx, 0, 1, 0);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mPyramidVetexBuffer);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, mPyramidColorBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6);

        gl.glPopMatrix();
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        // Cube
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        gl.glPushMatrix();
        gl.glTranslatef(1.0f, 0.0f, -5.0f);
        gl.glRotatef(rotx, 1, 0, 0);
        gl.glRotatef(roty, 0, 1, 0);

        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mCubeVetexBuffer);

        gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

        gl.glColor4f(1.0f, 0.5f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 4, 4);

        gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 8, 4);

        gl.glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 12, 4);

        gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 16, 4);

        gl.glColor4f(1.0f, 0.0f, 1.0f, 1.0f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 20, 4);

        gl.glPopMatrix();

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
   
        rotx += 2.0f;
        roty += 3.0f;
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        gl.glViewport(0, 0, width, height);
        GLU.gluPerspective(gl, 45.0f, 1.0f * width / height, 1.0f, 100.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

    }
}
